package com.chance.cc.crawler.prod.command.job.domain.vm.autohome.praiseZd;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.autohome.AutoHomeCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author: ZhaoHhuan on 2021/9/10 11:38
 * @Email: 18638575967@163.com
 * @Description:
 *      汽车之家口碑价格周度采集
**/
public class AutoHomeCarSeriesPraiseCrawlerScheduleJob extends AutoHomeCommonCrawlerSchedulerJob {
    public static String site = "carSeriesKouBeicarSeriesKouBei";//站点
    public static void main(String[] args) {
        publishAutoHomeCarSeriesPraiseCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishAutoHomeCarSeriesPraiseCrawlerScheduleJobInfo(){//发布
        CrawlerJob crawlerJob = autoHomeCarSeriesPraiseScheduler();//采集

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();//定时采集作业
        crawlerScheduleJob.setDomain(domain);//设置域
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));//设置采集信息
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());//工作标签
        crawlerScheduleJob.setNote("汽车之家车系口碑每周一1点定时采集");//采集提示
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());//生成爬虫key
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);//发布标签命令添加或修改脚本调度工作
        System.out.println("发布作业：" + page.getRawText());//打印文本
        return crawlerJob;//返回工作爬虫
    }

    /**
     * taobao采集
     * @return
     */
    public static CrawlerJob autoHomeCarSeriesPraiseScheduler() {//调度
        String url = "https://car.autohome.com.cn/javascript/NewSpecCompare.js?20131010";
//        String url="https://www.autohome.com.cn/135";
        String site_biz = site+ "-realtime";//实时
        HttpConfig httpConfig = HttpConfig.me(domain);//配置domain
        httpConfig.setHttpSite(site);//配置site


        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)//-
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .releaseTime(System.currentTimeMillis())
//                .filter(CrawlerEnum.CrawlerRecordFilter.key)
//                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1, null))
                .proxy(proxy)
                .build();
        crawlerRequestRecord.getHttpRequest().setResponseCharset("GBK");
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);//

        //请求队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue");//请求队列名

        //车系关键字
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("autohome_series_keywords",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domain+"/keys?site="+site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(10)//十个线程
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domain, site,"realtime", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))//请求队列
                .kafkaResultPipeline("kafka", kafkaTopciForTraceJob, null)//数据保存在kafka中
                .requestRecord(crawlerRequestRecord)
                .supportRecord(initNewsCrawlerRecord)
                .build();
        return crawlerJob;
    }

}
